package com.spelet.simulation;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.spelet.GamePreferences;
import com.spelet.VariableCollection;

public class Map {
	public final static int MIN_COLS = 4, MAX_COLS = 100;
	public final static int MIN_ROWS = 4, MAX_ROWS = 100;
	public int cols;
	public int rows;
	public String mapName;
	public TerrainBlock[][] terrainBlocks;
	public Block[][] blocks;
	public Array<Array<Block>> renderAtRows;;
	public Array<TerrainBlock> yellowpillars;
	public Array<TerrainBlock> yellowbuttons;
	public Array<TerrainBlock> bluepillars;
	public Array<TerrainBlock> bluebuttons;
	public FileHandle fileHandle;
	public Level[] levels;
	int currentLevel;
	public String[] levelData;
	private Simulation sim;
	public boolean[] levelsRead;
	public Map(Simulation sim) {
		this.sim = sim;
		levels = new Level[10];
		levelData = new String[10];
		levelsRead = new boolean[10];
	}
	public Map(){
		levels = new Level[11];
		levelData = new String[11];
		levelsRead = new boolean[11];
	}
	public void set(String ms,Character ch) {
		Level ls = new Level(sim,ms);
		ch.redKeys = 0;
		ch.blueKeys = 0;
		ch.yellowKeys = 0;
		rows = ls.rows;
		cols = ls.cols;
		blocks = ls.blocks;
		terrainBlocks = ls.terrainBlocks;
		yellowpillars = ls.yellowpillars;
		yellowbuttons = ls.yellowbuttons;
		bluepillars = ls.bluepillars;
		bluebuttons = ls.bluebuttons;
		renderAtRows = ls.renderAtRows;
		ch.setPosition(ls.startX, ls.startY);

	}
	public void loadLevel(Character ch, int level){
		ch.redKeys = 0;
		ch.blueKeys = 0;
		ch.yellowKeys = 0;
		currentLevel = level;
		String ls = "";
		if (levelsRead[level]) {
			ls = levelData[level];
		} else {
			if (level == 1) {
				fileHandle = Gdx.files.internal("1.Pushme");
				ls = fileHandle.readString();
			} else if (level == 2) {
				fileHandle = Gdx.files.internal("2.Bees,everywhere");
				ls = fileHandle.readString();
			} else if (level == 3) {
				fileHandle = Gdx.files.internal("3.Dirtymapisdirty");
				ls = fileHandle.readString();
			} else if (level ==4){
				fileHandle = Gdx.files.internal("4.WheretoStart");
				ls = fileHandle.readString();
			} else if (level ==5){
				fileHandle = Gdx.files.internal("5.Willitblend");
				ls = fileHandle.readString();
			} else if (level ==6){
				fileHandle = Gdx.files.internal("6.Bowlingkills");
				ls = fileHandle.readString();
			} else if (level ==7){
				fileHandle = Gdx.files.internal("7.Ifihadatower");
				ls = fileHandle.readString();
			} else if (level ==8){
				fileHandle = Gdx.files.internal("8.UMAdBro");
				ls = fileHandle.readString();
			} else if (level ==9){
				fileHandle = Gdx.files.internal("8");
				ls = fileHandle.readString();
			}
			levelData[level] = ls;
			levelsRead[level] = true;
		}
		GamePreferences.unlockLevel(level-1);
		levels[level] = new Level(sim,ls);
		rows = levels[level].rows;
		cols = levels[level].cols;
		blocks = levels[level].blocks;
		terrainBlocks = levels[level].terrainBlocks;
		yellowpillars = levels[level].yellowpillars;
		yellowbuttons = levels[level].yellowbuttons;
		bluepillars = levels[level].bluepillars;
		bluebuttons = levels[level].bluebuttons;
		renderAtRows = levels[level].renderAtRows;
		ch.setPosition(levels[level].startX, levels[level].startY);
	}
	public void MElvl(Character ch, String ls) {
		Level l = new Level(sim,ls);
		rows = l.rows;
		cols = l.cols;
		blocks = l.blocks;
		terrainBlocks = l.terrainBlocks;
		renderAtRows = l.renderAtRows;
		ch.setPosition(l.startX, l.startY);
	}
	public void restartLevel(Character ch) {

		loadLevel(ch,currentLevel);
	}

	public void swapCols(int src, int target) {
		Block[] tempBlocks = new Block[blocks[0].length];
		TerrainBlock[] tempTerrains = new TerrainBlock[terrainBlocks[0].length];

		tempBlocks = blocks[src];
		blocks[src] = blocks[target];
		blocks[target] = tempBlocks;

		tempTerrains = terrainBlocks[src];
		terrainBlocks[src] = terrainBlocks[target];
		terrainBlocks[target] = tempTerrains;

		int blength = blocks[src].length;

		if(blength != blocks[target].length || blength != terrainBlocks[src].length || blength != terrainBlocks[target].length)
			throw new RuntimeException("YOU SHALL NOT PASS.");

		for(int y=0;y<blength;y++) {
			blocks[src][y].tileX = src;
			blocks[src][y].tileY = y;
			blocks[target][y].tileX = target;
			blocks[target][y].tileY = y;
			terrainBlocks[src][y].spriteX = src*64;
			terrainBlocks[src][y].spriteY = y*64;
			terrainBlocks[target][y].spriteX = target*64;
			terrainBlocks[target][y].spriteY = y*64;
		}
	}

	public void swapRows(int src, int target) {
		Block tempBlock;
		TerrainBlock tempTerrain;

		for(int x=0; x < cols; x++) {
			tempBlock = blocks[x][src];
			blocks[x][src] = blocks[x][target];
			blocks[x][target] = tempBlock;

			tempTerrain = terrainBlocks[x][src];
			terrainBlocks[x][src] = terrainBlocks[x][target];
			terrainBlocks[x][target] = tempTerrain;

			blocks[x][src].tileX = x;
			blocks[x][src].tileY = src;
			blocks[x][target].tileX = x;
			blocks[x][target].tileY = target;
			terrainBlocks[x][src].spriteX = x*64;
			terrainBlocks[x][src].spriteY = src*64;
			terrainBlocks[x][target].spriteX = x*64;
			terrainBlocks[x][target].spriteY = target*64;
		}

		renderAtRows.get(src).clear();
		renderAtRows.get(target).clear();

		for(int x=0;x<cols;x++)
		{
			renderAtRows.get(src).add(blocks[x][src]);
			renderAtRows.get(target).add(blocks[x][target]);
		}
	}

	public void addCol() {
		Block[][] newBlocks = new Block[cols+1][rows];
		TerrainBlock[][] newTerrain = new TerrainBlock[cols+1][rows];
		for (int x = 0; x < cols; x++) {
			newBlocks[x] = blocks[x];
			newTerrain[x] = terrainBlocks[x];
		}
		for (int y = 0; y < rows; y++) {
			newBlocks[cols][y] = new Block(VariableCollection.nothing,cols,y);
			newTerrain[cols][y] = new TerrainBlock(VariableCollection.grass,cols,y);
		}
		cols++;
		blocks = newBlocks;
		terrainBlocks = newTerrain;
		for (int x = 0; x < cols; x++) {
			for (int y = 0; y < rows; y++) {
				blocks[x][y].tileX = x;
				blocks[x][y].tileY = y;
				terrainBlocks[x][y].spriteX = x*64;
				terrainBlocks[x][y].spriteY = y*64;
			}
		}
	}
	public void removeCol() {
		if (cols <= 1)
			return;
		cols--;
		Block[][] newBlocks = new Block[cols][rows];
		TerrainBlock[][] newTerrain = new TerrainBlock[cols][rows];
		for (int x = 0; x < cols; x++) {
			newBlocks[x] = blocks[x];
			newTerrain[x] = terrainBlocks[x];
		}
		blocks = newBlocks;
		terrainBlocks = newTerrain;
	}
	public String toString(Character ch) {
		String returnTerrain = "";
		String returnBlocks = "";
		for (int y = rows-1; y >= 0; y--) {
			for (int x = 0; x < cols; x++) {
				if (ch.tileX == x && ch.tileY == y) {
					returnTerrain += terrainBlocks[x][y].toString();
					returnBlocks += 'c';
				} else {
					returnTerrain += terrainBlocks[x][y].toString();
					returnBlocks += blocks[x][y].toString();
				}
				if (x < cols-1) {
					returnTerrain += " ";
					returnBlocks += " ";
				}
			}
			if (y > 0) {
				returnTerrain += "\n";
				returnBlocks += "\n";
			}
		}
		return returnTerrain + "\nLayer\n" + returnBlocks;
	}
}